home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / berm122 / utextra.c < prev    next >
C/C++ Source or Header  |  1993-08-16  |  15KB  |  692 lines

  1. /*******************************************************************
  2.  *  UTEXTRA.C                                                      *
  3.  *                                                                 *
  4.  * This file contains misc. functions needed for the BERMUDA       *
  5.  * software. See LICENSE for information about distribution.       *
  6.  *                                                                 *
  7.  * Written by Jac Kersing                                          *
  8.  *******************************************************************/
  9.  
  10. /*
  11. #pragma warn -par
  12. #pragma warn -sus
  13. */
  14.  
  15. #define MAILER        /* VP */
  16. #include "pandora.h"
  17.  
  18. #if     TC
  19. #include <dir.h>
  20. #include <dos.h>
  21. #include <sys\stat.h>
  22. #endif
  23.  
  24. #if UNIX
  25. #include <sys/dir.h>
  26. #include <time.h>
  27. #endif
  28.  
  29. #if STTC
  30. #include <ext.h>
  31. #include <tos.h>
  32. #endif
  33.  
  34. #if    BWIN32
  35. #include <process.h>
  36. #include <time.h>
  37. #include <io.h>
  38. #endif
  39.  
  40. #define __PROTO__ 1
  41. #include "utextra.pro"
  42.  
  43. extern int loglevel;
  44. extern FILE *log;                       /* used for logging */
  45.  
  46. #define LP              *(unsigned long *)
  47. #define IP              (unsigned long)*(unsigned int *)
  48.  
  49.  
  50. void setstamp(name,buf) char *name,*buf;
  51. {
  52. #if     MEGAMAX
  53.         int f;
  54.         if((f=Fopen(name,2))<0) return;
  55.         Fdatime(f,buf,1);
  56.         Fclose(f);
  57. #endif
  58. }
  59.  
  60. #ifdef LINN
  61. static void conv(getal,p,base,unsign,low)   /* Local function of doprint    */
  62. int low;
  63. #else
  64. static void conv(getal,p,base,unsign)   /* Local function of doprint    */
  65. #endif
  66. unsigned long getal;                    /* convert number to ascii      */
  67. char *p;
  68. int base;
  69. int unsign;
  70. {
  71.         char buf[10], *t = &buf[9], sign=getal&0x80000000L && !unsign;
  72.         char ch;
  73.         *t = 0;
  74.         if (sign) getal = -((long)getal);
  75.         do {
  76.                 ch=(char)(getal % base);
  77. #ifdef LINN
  78.                 *--t = (ch < 10) ? ch+'0' : ch-10+(low?'a':'A');
  79. #else
  80.                 *--t = (ch < 10) ? ch+'0' : ch-10+'A';
  81. #endif
  82.                 getal /= base;
  83.         }
  84.         while (getal);
  85.         if (sign) *--t='-';
  86.         strcpy(p,t);
  87. }
  88.  
  89. char *mon[12] =
  90. {
  91.         "Jan","Feb","Mar","Apr","May","Jun",
  92.         "Jul","Aug","Sep","Oct","Nov","Dec"
  93. };
  94.  
  95. static void cdecl iprint(outp,fmt,arg)        /* needed for internal print    */
  96. #if __PROTO__
  97. void (*outp)(char);
  98. #else
  99. void (*outp)();
  100. #endif
  101. char *fmt, *arg;
  102. {
  103.         doprint(outp, fmt, &arg);
  104. }
  105.  
  106. void cdecl doprint(outp,fmt,arg)                   /* Print internal format        */
  107. #if __PROTO__
  108. void (*outp)(char);
  109. #else
  110. void (*outp)();
  111. #endif
  112. char *fmt, *arg;
  113. {
  114.         register int  width, precision, uselong;
  115.         register char *p,*pos,left,dot,pad;
  116.         char    buf[128];
  117.         struct date d;
  118.         struct time t;
  119.  
  120.         getdate(&d);
  121.         gettime(&t);
  122.  
  123.  p = fmt-1;
  124.  while (*++p) {
  125.   if (*p=='%') {
  126.    p++;
  127.    uselong=width=precision=0;
  128.    pad = ' ';
  129.    if (*p=='%') { (*outp)('%'); continue; }
  130.    left = *p=='-';
  131.    if ( left ) p++;
  132.    if ( *p=='0' ) pad='0';
  133.    while ( isdigit(*p) ) width = width * 10 + *p++ - '0';
  134.    dot = *p=='.';
  135.    if ( dot ) {
  136.     p++;
  137.     while ( isdigit(*p) ) precision = precision * 10 + *p++ - '0';
  138.    }
  139.    uselong= *p=='l';
  140.    if ( uselong ) p++;
  141.    switch (*p) {
  142.     case 'D' :  uselong++;
  143.     case 'd' :  conv( uselong ? *(long *)arg : (long)*(int *)arg, buf, 10, 0, 0);
  144.                 arg += uselong ? sizeof(long) : sizeof(int);
  145.                 break;
  146.     case 'U' :  uselong++;
  147.     case 'u' :  conv( uselong ? LP arg : IP arg, buf, 10, 1, 0);
  148.                 arg += uselong ? sizeof(long) : sizeof(int);
  149.                 break;
  150.     case 'X' :  uselong++;
  151.     case 'x' :  conv( uselong ? LP arg : IP arg, buf, 0x10, 1, 0);
  152.                 arg += uselong ? sizeof(long) : sizeof(int);
  153.                 break;
  154. #ifdef LINN
  155.     /* Y is lowercase hexadecimal, for use while numbering outbound directory a la Binkley */
  156.     case 'Y' :  uselong++;
  157.     case 'y' :  conv( uselong ? LP arg : IP arg, buf, 0x10, 1, 1);
  158.                 arg += uselong ? sizeof(long) : sizeof(int);
  159.                 break;
  160. #endif
  161.     case 'O' :  uselong++;
  162.     case 'o' :  conv( uselong ? LP arg : IP arg, buf, 8, 1, 0);
  163.                 arg += uselong ? sizeof(long) : sizeof(int);
  164.                 break;
  165.     case 'Z' :  uselong++;
  166.     case 'z' :  conv( uselong ? LP arg : IP arg, buf, 36, 1, 0);
  167.                 arg += uselong ? sizeof(long) : sizeof(int);
  168.                 break;
  169.     case 'c' :  buf[0] = *(int *)arg;
  170.                 buf[1] = 0;
  171.                 arg += sizeof(int);             /* Char on stack == int */
  172.                 break;
  173.     case 's' :  strncpy(buf,*(char **)arg,127);
  174.                 buf[127]=0;
  175.                 arg += sizeof(char *);
  176.                 if (dot) buf[precision] = 0;
  177.                 break;
  178.    }
  179.    width -= (int)strlen(buf);
  180.    if (!left) while (width-->0) (*outp)(pad);   /* left padding         */
  181.    pos=buf;
  182.    while (*pos) (*outp)( *pos++ );              /* buffer               */
  183.    if (left) while (width-->0) (*outp)(pad);    /* right padding        */
  184.    continue;
  185.   }
  186.   if (*p=='$')
  187.    switch (*++p) {
  188.     case '$' : (*outp)('$'); break;
  189.     case 't' : iprint(outp, "%02d:%02d:%02d", t.ti_hour, t.ti_min, t.ti_sec); break;
  190.     case 'd' : iprint(outp, "%02d-%02d-%02d", d.da_day, d.da_mon, d.da_year-1900); break;
  191.     case 'D' : iprint(outp, "%02d", d.da_day); break;
  192.     case 'm' : iprint(outp, "%03s", mon[d.da_mon-1]); break;
  193. #ifdef LINN    /* some more options used in pack.c */
  194.     case 'T' : iprint(outp, "%02d:%02d", t.ti_hour, t.ti_min); break;
  195.     case 'y' : iprint(outp, "%02d", d.da_year); break;
  196. #endif
  197.   }
  198.   else (*outp)(*p);
  199.  }
  200. }
  201.  
  202. static char *prtstr[2];                 /* Must be array: Time/Date!    */
  203. static int prtcnt = -1;                 /* points to current buffer     */
  204.  
  205. static void putSTR(c)                   /* Append char to string        */
  206. char c;
  207. {
  208.         *prtstr[prtcnt]++ = c;
  209. }
  210.  
  211. void cdecl fmprint(buf,fmt,args)              /* print format to string       */
  212. char *buf, *fmt, *args;
  213. {
  214.         prtstr[++prtcnt] = buf;
  215.         doprint(putSTR, fmt, args);
  216.         *prtstr[prtcnt--] = 0;          /* add string terminator        */
  217. }
  218.  
  219. void cdecl sprint(buf,fmt,args)               /* print format to string       */
  220. char *buf, *fmt, *args;
  221. {
  222.         prtstr[++prtcnt] = buf;
  223.         doprint(putSTR, fmt, &args);
  224.         *prtstr[prtcnt--] = 0;          /* add string terminator        */
  225. }
  226.  
  227. extern char progname[];
  228.  
  229. void cdecl message(level,fmt,arg)             /* display a message to screen */
  230. int level;                              /* and to logfile */
  231. char *fmt, *arg;
  232. {
  233.     char buf[255];
  234.  
  235.     sprint(buf,"%c $D $m $t %3.3s  ", *fmt,progname);
  236.     fmprint(&buf[strlen(buf)], &fmt[1], &arg);
  237.  
  238.     if (level>=loglevel && log)
  239.     {
  240.         fprintf(log, "%s\n", buf);
  241.         fflush(log);
  242.     }
  243.  
  244.     print("%s\n",buf);
  245. }
  246.  
  247. void cdecl clprint(fmt,arg)
  248. char *fmt, *arg;
  249. {
  250.     char buf[255];
  251.  
  252.     fmprint(buf, fmt, &arg);
  253.  
  254.     if (loglevel<1 && log)
  255.     {
  256.         fprintf(log, "%s", buf);
  257.         fflush(log);
  258.     }
  259.  
  260.     print("%s",buf);
  261. }
  262.  
  263. void cdecl print(fmt, args)
  264. char *fmt, *args;
  265. {
  266. #if     MEGAMAX | MWC
  267.         char buffer[255];
  268.  
  269.         fmprint(buffer, fmt, &args);
  270.         fprintf(stdout,"%s", buffer);
  271.         fflush(stdout);
  272. #endif
  273. #if     TC | STTC | UNIX | BWIN32
  274.         vprintf(fmt, &args);
  275. #endif
  276. }
  277.  
  278. static char fname[20];  /* name of the file found by ffirst, fnext */
  279. #if     MEGAMAX | MWC
  280. static char tdta[80];
  281. #endif
  282. #if     TC | STTC
  283. static  struct ffblk FF;
  284. #endif
  285. #if        BWIN32
  286. static    struct _finddata_t FF;
  287. static  long    win32fh;
  288. #endif
  289.  
  290. #if    UNIX
  291. /* from Ben Stuyts's port of Binkley */
  292. char curr_filename[256],
  293.      curr_pathname[256];
  294. DIR *dirp = NULL;
  295.  
  296. #define FILENAMELEN 13
  297.  
  298. void unixize(char *dos, char *un)
  299. {
  300.     char *s, *d, ch;
  301.  
  302.     s = dos;    /* source */
  303.     d = un;        /* dest */
  304.     while(*s) {
  305.         ch = *s++;
  306.         switch(ch) {
  307.             case '\\':
  308.                 *d++ = '/';        /* first change all \ into / */
  309.                 break;
  310.             case '?':
  311.                 *d++ = '.';        /* any char as ? */
  312.                 break;
  313.             case '.':
  314.                 *d++ = '\\';    /* dot as \. */
  315.                 *d++ = '.';
  316.                 break;
  317.             case '*':
  318.                 *d++ = '.';        /* any seq of char as .* */
  319.                 *d++ = '*';
  320.                 break;
  321.             default:
  322.                 if (isalpha(ch)) {
  323.                     /* we do pattern matching case insensitive */
  324.                     *d++ = '[';
  325.                     *d++ = toupper (ch);
  326.                     *d++ = tolower (ch);
  327.                     *d++ = ']';
  328.                 } else
  329.                     *d++ = ch;
  330.                 break;
  331.         }
  332.     }
  333.     *d++ = '$';    /* end of line marker */
  334.     *d = '\0';
  335. }
  336.  
  337. void splitpath(char *path, char *file, char *dir)
  338. {
  339.     char *pos;
  340.  
  341.     file[0] = '\0';
  342.     dir[0] = '\0';
  343.  
  344.     pos = rindex(path, '/');
  345.     if (pos == 0)
  346.         strcpy(file, path);
  347.     else {
  348.         *pos = '\0';
  349.         strcpy(dir, path);
  350.         pos++;
  351.         strcpy(file, pos);
  352.     }
  353. }
  354.  
  355. /* checkfile returns TRUE when filename is correct */
  356. int checkfile(struct direct *dp)
  357. {
  358.     char name[20];
  359.     int res;
  360.  
  361. #ifdef RE_BSD
  362.     re_comp(curr_filename);
  363. #endif
  364.  
  365.     if(dp->d_namlen > FILENAMELEN)
  366.         res = 0;
  367.     else {
  368.         strncpy(name, dp->d_name, dp->d_namlen);
  369.         name[dp->d_namlen] = '\0';
  370. #ifdef RE_BSD
  371.         res = re_exec(name);
  372. #else
  373.         res = !recmp(curr_filename, name);
  374. #endif
  375.     }
  376.     return res;
  377. }
  378. #endif    /* unix */
  379.  
  380. char *ffirst(name)
  381. char *name;
  382. {
  383. #if     MEGAMAX | MWC
  384.         char *hisdta= (char *)Fgetdta();        /* file info stored here */
  385.         char *q,*p;                             /* used for copying */
  386.         char *temp;
  387.  
  388.         Fsetdta(tdta);
  389.  
  390.         if (Fsfirst(name,7)!=-33l)
  391.         {
  392.                 p=fname;
  393.                 for (q=(tdta+30);*q;) *p++=*q++;
  394.                 *p='\0';
  395.                 temp= fname;
  396.         }
  397.         else
  398.                 temp= NULL;
  399.  
  400.         Fsetdta(hisdta);
  401.         return temp;
  402. #endif
  403. #if     TC | STTC
  404.         if (findfirst(name, &FF, 0x27)==0)
  405.         {
  406.                 strcpy(fname, FF.ff_name);
  407.                 return fname;
  408.         }
  409.         return NULL;
  410. #endif
  411. #if        UNIX
  412.     char un[256];
  413.     struct direct *dp;
  414.  
  415.     unixize(name, un);
  416.     splitpath(name, un, curr_pathname);
  417.     unixize (un, curr_filename);
  418.  
  419.     if(dirp)
  420.         closedir(dirp); /* cleanup for previous usage */
  421.  
  422.     dirp = opendir(curr_pathname);
  423.     if (!dirp)
  424.         return NULL;
  425.     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  426.         if (checkfile(dp))
  427.             return dp->d_name;
  428.     closedir(dirp);
  429.     dirp = NULL;
  430.     return NULL;
  431. #endif
  432. #if        BWIN32
  433.         if ((win32fh=_findfirst(name, &FF))>=0)
  434.         {
  435.                 strcpy(fname, FF.name);
  436.                 return fname;
  437.         }
  438.         return NULL;
  439. #endif
  440. }
  441.  
  442. char *fnext()
  443. {
  444. #if     MEGAMAX | MWC
  445.         char *hisdta= (char *)Fgetdta();        /* file info stored here */
  446.         char *q,*p;                             /* used for copying */
  447.         char *temp;
  448.  
  449.         Fsetdta(tdta);
  450.  
  451.         if (Fsnext()==0l)
  452.         {
  453.                 p=fname;
  454.                 for (q=(tdta+30);*q;) *p++=*q++;
  455.                 *p='\0';
  456.                 temp= fname;
  457.         }
  458.         else
  459.                 temp= NULL;
  460.  
  461.         Fsetdta(hisdta);
  462.         return temp;
  463. #endif
  464. #if     TC | STTC
  465.         if (findnext(&FF)==0)
  466.         {
  467.                 strcpy(fname, FF.ff_name);
  468.                 return fname;
  469.         }
  470.         return NULL;
  471. #endif
  472. #if    UNIX
  473.     struct direct *dp;
  474.  
  475.     if(!dirp)
  476.         return NULL;
  477.  
  478.     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  479.         if (checkfile(dp))
  480.             return dp->d_name;
  481.     closedir(dirp);
  482.     dirp = NULL;
  483.     return NULL;
  484. #endif
  485. #if BWIN32
  486.         if(win32fh<0)
  487.             return NULL;
  488.         if (_findnext(win32fh,&FF)==0)
  489.         {
  490.                 strcpy(fname, FF.name);
  491.                 return fname;
  492.         }
  493.         _findclose(win32fh);
  494.         return NULL;
  495. #endif
  496. }
  497.  
  498. static char *suffixes[8] = {
  499.    "SU", "MO", "TU", "WE", "TH", "FR", "SA", NULL
  500. };
  501.  
  502. #if     MEGAMAX || MSTRUPR
  503. char *strupr(str)
  504. char *str;
  505. {
  506.         char *p=str;
  507.         while (*p)
  508.         {
  509.                 if (isalpha(*p)) *p=toupper(*p);
  510.                 ++p;
  511.         }
  512.         return str;
  513. }
  514. #endif
  515.  
  516. #if MSTRDUP
  517. char *strdup (char *s)
  518. {
  519.     char *t;
  520.     t=malloc (strlen(s)+1);
  521.     if (t != NULL)
  522.         strcpy (t, s);
  523.     return t;
  524. }
  525. #endif
  526.  
  527. #if UNIX | BWIN32
  528. void getdate (struct date *date)
  529. {
  530.     struct tm *tim;
  531.     time_t ze_time;
  532.  
  533.     time (&ze_time);
  534.     tim = localtime (&ze_time);
  535.     date->da_year = tim->tm_year;
  536.     date->da_day = tim->tm_mday;
  537.     date->da_mon = tim->tm_mon+1;
  538. }
  539.  
  540. void gettime (struct time *timee)
  541. {
  542.     struct tm *tim;
  543.     time_t ze_time;
  544.  
  545.     time (&ze_time);
  546.     tim = localtime (&ze_time);
  547.     timee->ti_min = tim->tm_min;
  548.     timee->ti_hour = tim->tm_hour;
  549.     timee->ti_hund = 0;
  550.     timee->ti_sec = tim->tm_sec;
  551. }
  552. #endif
  553.  
  554. int is_arcmail (p, n)
  555. char *p;
  556. int n;
  557. {
  558.    int i;
  559.    char c[128];
  560.  
  561.    if (!isdigit (p[n]))
  562.       {
  563.       return (0);
  564.       }
  565.  
  566.    strcpy (c, p);
  567.    strupr (c);
  568.  
  569.    for (i = n - 11; i < n - 3; i++)
  570.       {
  571.       if ((!isdigit(c[i])) && ((c[i] > 'F') || (c[i] < 'A')))
  572.          return(0);
  573.       }
  574.  
  575.    for (i = 0; i < 7; i++)
  576.       {
  577.       if (strnicmp (&c[n-2], suffixes[i], 2) == 0)
  578.          break;
  579.       }
  580.  
  581.    if (i >= 7)
  582.       {
  583.       return(0);
  584.       }
  585.  
  586.    return (1);
  587. }
  588.  
  589. int execute(prg,cmd)
  590. char *prg, *cmd;
  591. {
  592. #if     TC | BWIN32
  593.     char *arg[20], *p;
  594.     int i;
  595. #endif
  596.     long result;
  597.  
  598. #if     MEGAMAX
  599.     extern char *xenvistr;
  600.  
  601.     result= Pexec(0,prg,cmd,xenvistr);
  602.     if (result < -30L)
  603. #endif
  604. #if     MWC | STTC
  605.     extern char *environ;
  606.  
  607.     result= Pexec(0,prg,cmd,environ);
  608.     if (result < -30L)
  609. #endif
  610. #if     TC | BWIN32
  611.     i=2;
  612.     arg[0]=prg;
  613.     arg[1]=&cmd[1];
  614.     for (p=&cmd[1]; *p; p++)
  615.       if (isspace(*p))
  616.       {
  617.          *p='\0';
  618.          arg[i++]=p+1;
  619.          while (isspace( *(p+1) )) p++;
  620.       }
  621.     arg[i]=NULL;
  622.     result = spawnv(0, prg, arg);
  623.     if (result)
  624. #endif
  625. #if UNIX
  626.     char tot[200];
  627.     sprintf (tot, "%s %s", prg, cmd+1);    /* skip length of command */
  628.     result = system(tot);
  629.     if (result)
  630. #endif
  631.     {
  632.         message(6,"!Program %s not found!", prg);
  633.     }
  634.     return (int)result;
  635. }
  636.  
  637. int weekday(d) struct date *d;
  638. {
  639.         unsigned long df1;
  640.         unsigned long day, month, year;
  641.  
  642.         day=d->da_day;
  643.         month=d->da_mon;
  644.         year=d->da_year;
  645.  
  646.         df1= 365l*year + 31L * (month-1) + day + 1;
  647.         if (month>2)
  648.         {
  649.                 df1 -= (4 * month + 23 ) / 10;
  650.                 ++year;
  651.         }
  652.         df1 += (year-1)/4;
  653.         df1 -= ( 3 * ((year-1) / 100) + 1 ) / 4; /* friday == 1 */
  654.         df1 += 4;
  655.  
  656.         return (int) (df1%7);
  657. }
  658.  
  659. unsigned int ztoi(str,len)
  660. char *str;
  661. int len;
  662. {
  663.         unsigned int temp;
  664.  
  665.         temp=0;
  666.         while (*str && len-- && isalnum(*str))
  667.         {
  668.                 temp *= 36;
  669.                 temp += isdigit(*str) ? *str-'0' : toupper(*str)-'A'+10;
  670.                 ++str;
  671.         }
  672.         return temp;
  673. }
  674.  
  675. int getadress(str, zone, net, node)
  676. char *str;
  677. int *net, *node, *zone;
  678. {
  679.         *zone= ztoi(str,2);
  680.         *net = ztoi(str+2,3);
  681.         *node= ztoi(str+5,3);
  682.         return 0;
  683. }
  684.  
  685. #if MC68000==1    /* changed from STTC vp */
  686. unsigned inteli(x)
  687. int x;
  688. {
  689.     return ( ((x << 8) & 0xFF00) | ((x >> 8) & 0x00FF) );
  690. }
  691. #endif
  692.